//
//  GQHTransactionInBankView.m
//  Xanthium
//
//  Created by GuanQinghao on 28/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHTransactionInBankView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"

@interface GQHTransactionInBankView () <UITableViewDelegate,UITableViewDataSource>

@end


@implementation GQHTransactionInBankView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kAppleGrayColor;
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kLightGrayColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

#pragma mark --Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return MAX(_qh_contentArray.count, 1);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_qh_contentArray && _qh_contentArray.count > 0) {
        
        return 100.0f;
    } else {
        
        return 60.0f;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_qh_contentArray && _qh_contentArray.count) {
        
        static NSString *identifier = @"aCell";
        GQHTransactionInBankCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHTransactionInBankCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
        NSDictionary *inBankDictionary = _qh_contentArray[indexPath.row];
        NSString *imageString = [inBankDictionary objectForKey:@"reserved3"];
        
        NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:imageString];
        NSURL *URL = [NSURL URLWithString:URLString];
        [cell.qh_imageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
        
        cell.qh_captionLabel.text = [inBankDictionary objectForKey:@"openAccountBank"];
        cell.qh_detailLabel.text =  [inBankDictionary objectForKey:@"cardNum"];
        
        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.userInteractionEnabled = NO;
        }
        
        cell.imageView.image = [UIImage imageNamed:@"me_transaction_bank"];
        cell.textLabel.text = @"暂无银行账号信息";
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_qh_delegate respondsToSelector:@selector(qh_selectedRowWithTableView:atIndexPath:)]) {
        
        [_qh_delegate qh_selectedRowWithTableView:tableView atIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    UILabel *headerLabel = [[UILabel alloc] init];
    headerLabel.text = @"智运平台转账银行账号";
    headerLabel.font = kDefaultFont;
    headerLabel.textColor = kDarkGrayColor;
    headerLabel.textAlignment = NSTextAlignmentLeft;
    [headerView addSubview:headerLabel];
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(headerView).with.inset(GQH_MARGIN_SINGLE);
        make.top.and.bottom.mas_equalTo(headerView);
    }];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 150.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = kLightGrayColor;
    
    UILabel *footerLabel = [[UILabel alloc] init];
    footerLabel.text = @"请按照以下步骤提交转账信息：\n\n1.点击选择转入的智云平台银行账号；\n2.填写转账详细信息，点击提交；\n\n转账信息提交后，我们将在1-2个工作日为您确认。";
    footerLabel.font = kDefaultFont;
    footerLabel.textColor = kDarkGrayColor;
    footerLabel.textAlignment = NSTextAlignmentLeft;
    footerLabel.numberOfLines = 0;
    [footerView addSubview:footerLabel];
    [footerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(footerView).with.inset(GQH_MARGIN_SINGLE);
        make.top.and.bottom.mas_equalTo(footerView);
    }];
    
    return footerView;
}

@end
